package com.jingzhi.count.controller;

import com.jingzhi.count.entity.MyResponse;
import com.jingzhi.count.entity.Team;
import com.jingzhi.count.mapper.TeamMapper;
import com.jingzhi.count.model.Paging;
import com.jingzhi.count.service.TeamService;
import com.jingzhi.count.utils.CommonUtils;
import com.jingzhi.count.utils.TextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 作者： 张恒同
 * 时间： 2018/3/29   11:29
 * 描述： 班组
 */
@RestController
@RequestMapping("/team")
public class TeamController extends BaseController {
    @Autowired
    private TeamMapper mapper;
    @Autowired
    private TeamService teamService;


    @PostMapping(value = "/addTeam.do")
    public ResponseEntity<MyResponse> addTeam(@RequestParam(value = "name", required = true) String name,
                                              @RequestParam(value = "account", required = false) String account) {

        MyResponse response = new MyResponse();

        Team temp = mapper.selectByName(name);
        if (temp != null) {
            response.setRc(MyResponse.ERROR);
            response.setDes("已存在该班组");
            return new ResponseEntity<MyResponse>(response, HttpStatus.OK);
        }

        Team team = new Team();
        team.setName(name);
        if (TextUtil.isEmpty(account)) {
            team.setAccount(name);
        } else {
            team.setAccount(account);
        }
        team.setPassword(CommonUtils.encyptBySha1("123456"));
        mapper.insert(team);

        response.setRc(MyResponse.SUCCESS);
        response.setDes("添加班组成功");

        return new ResponseEntity<MyResponse>(response, HttpStatus.OK);
    }

    /**
     * 分页查询
     * @param page
     * @param limit
     * @return
     */
    @GetMapping(value = "/getTeams")
    public MyResponse<Paging<Team>> getTeams(Integer page,Integer limit,@RequestAttribute Team team) {
        System.out.println(team.getAccount());
        MyResponse<Paging<Team>> response = new MyResponse();
        Paging<Team> paging = new Paging<>(page, limit);
        teamService.selectAllPage(paging);
        response.isSuccess("");
        response.setData(paging);
        return response;
    }

    /**
     * 登陆
     * @param account
     * @param password
     * @return
     */
    @PostMapping(value = "login")
    public MyResponse teamLogin(@RequestParam(value = "account") String account,
                                @RequestParam(value = "password") String password) {
        MyResponse<String> response = new MyResponse();
        try {
            if (StringUtils.isBlank(account)) {
                response.isError("用户名不能为空");
                return response;
            }
            if (StringUtils.isBlank(password)) {
                response.isError("密码不能为空");
                return response;
            }
            Team team = new Team();
            team.setAccount(account);
            team.setPassword(password);
            String rs = teamService.login(team);
            if (StringUtils.isBlank(rs)) {
                response.isSuccess("登陆成功");
                response.setData(team.getToken());
            } else {
                response.isError(rs);
            }
        }catch (Exception ex){
            logger.error("登陆异常，原因：{}",ex.getStackTrace());
            ex.printStackTrace();
            response.isError("登陆失败");
        }
        return response;
    }

}
